package com.example.demo.interceptor;

import com.example.demo.entity.Admin;
import com.example.demo.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author shenrui
 * @date 2021/9/20
 * @description 对user的拦截器
 */
public class UserLoginInterceptor implements HandlerInterceptor {
    /**
     *@description 在进入前判断用户是否登录
     * @return bolean
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        User user = (User) request.getSession().getAttribute("userLogin");
        if(user != null) {
            return true;
        } else {
            request.setAttribute("userMsg","请先登录");
            request.getRequestDispatcher("/user_login").forward(request,response);
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
